package com.sun.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.common.Result;
import com.sun.common.SystemCrmlog;
import com.sun.dto.OutstoreDto;
import com.sun.service.OutstoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collections;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/3/26 17:47
 * @FileName: OutstoreController
 * @Description: ""
 */
@RestController
@RequestMapping("/outStore")
public class OutstoreController {
    @Autowired
    private OutstoreService outstoreService;

    /**
     * 新增出库单
     */
    @SystemCrmlog(title = "出库管理模块", type = "INSERT", description = "新增出库单")
    @PostMapping
    public Result<String> save(@RequestBody OutstoreDto outstoreDto) {
        outstoreService.saveWithOutstoreDetail(outstoreDto);
        return Result.success("新增入库单成功");
    }

    /**
     * 修改出库单
     */
    @SystemCrmlog(title = "出库管理模块", type = "UPDATE", description = "修改出库单")
    @PutMapping
    public Result<String> update(@RequestBody OutstoreDto outstoreDto) {

        outstoreService.updateWithOutstoreDetail(outstoreDto);

        return Result.success("出库单信息修改成功");
    }

    /**
     * 分页查询(可以根据出库员姓名、出库类型、时间进行条件查询）
     */
    @GetMapping("/page")
    public Result<IPage> page(int pageNum, int pageSize,
                              String userName, String addr,
                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<OutstoreDto> pageInfo = new Page<>(pageNum, pageSize);

        IPage<OutstoreDto> outStoreDtoPage = outstoreService.selectOutstoreDtoListPage(pageInfo, userName, addr, startTime, endTime);

        return Result.success(outStoreDtoPage);
    }

    /**
     * 根据id查询入库单的相关信息
     */
    @GetMapping("/{id}")
    public Result<OutstoreDto> get(@PathVariable Long id) {
        OutstoreDto outstoreDto = outstoreService.getByIdWithOutstoreDetail(id);
        return Result.success(outstoreDto);
    }

    /**
     * 删除出库单
     */
    @SystemCrmlog(title = "出库管理模块", type = "DELETE", description = "删除出库单")
    @DeleteMapping("/{ids}")
    public Result<String> batchDelete(@PathVariable Long ids) {
        outstoreService.batchDeleteByIds(Collections.singletonList(ids));
        return Result.success("成功删除出库单！");
    }
}
